/**
 * Created by fdg282 on 2017/8/7.
 */
$(document).ready(function(){
	$('#category-list-form').on('click','.screen-class',function(){
        var goods_count = $('#goods_count').val();
        if(goods_count == 0){
            return false;
        }
        var li_sort_type = $(this).attr('data-sort-type');
        var sort_type = $('#sort_type').val();
        var sort_val = $('#sort_val').val();

        if(li_sort_type == 'default'){
            $('#sort_val').val(2);
            $('.tool_list .screen-class span').html('');
        }else{
            $('#icon_'+li_sort_type).empty();
            if(li_sort_type == sort_type && sort_val == 1){
                $('#sort_val').val(2);
                $('#icon_'+li_sort_type).html('↓');
            }else{
                $('.tool_list .screen-class span').html('');
                $('#sort_val').val(1);
                $('#icon_'+li_sort_type).html('↑');
            }
        }
        $('#sort_type').val(li_sort_type);
		getGoods();
	});
	getGoods();
});
//mod by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 start
function getGoods(page){
    var goods_count = $('#goods_count').val();
    if(goods_count == 0){
        return false;
    }
	if(page == undefined){
		var page = 1;
	}
	var form = $('#category-list-form');
	$.post(form.attr('action')+'?page='+page,form.serialize(),success,'json');
	function success(data){
        if(data.code == 200){
            var return_html = data.val;
        	$('.pagination_list').html(return_html.pagination);
            $('#category_goods_list').html(return_html.html);
            $('#category_goods_list').find('span.icon_animation').on('click',function(){
                var goods_id = $(this).attr('data-goods-id');
                $.post('/goods/goods-collection',{goods_id:goods_id},function success(data){
                    if(data.code == 500){
                        window.location.href = data.url;
                    }
                    if(data.code == 200 || data.code == 202){
                        var count = parseInt($("#hart-count").text());
                        $('#category_goods_list').find('span.icon_animation').each(function(){
                            if($(this).attr('data-goods-id') == goods_id){
                                if($(this).is(".icon-hart-active")){
                                    count--;
                                    $(this).removeClass('icon-hart-active').addClass('icon-hart');
                                    $("#hart-count").text(count);
                                }else {
                                    $(this).removeClass('icon-hart').addClass('icon-hart-active');
                                    gohartAnimate(this);
                                }
                            }
                        });
                    }
                },'json');
            });

            $('#group_goods_list').find('span.icon_animation').on('click',function(){
                var goods_id = $(this).attr('data-goods-id');
                $.post('/goods/goods-collection',{goods_id:goods_id},function success(data){
                    if(data.code == 500){
                        window.location.href = data.url;
                    }
                    if(data.code == 200 || data.code == 202){
                        var count = parseInt($("#hart-count").text());
                        $('#group_goods_list').find('span.icon_animation').each(function(){
                            if($(this).attr('data-goods-id') == goods_id){
                                if($(this).is(".icon-hart-active")){
                                    count--;
                                    $(this).removeClass('icon-hart-active').addClass('icon-hart');
                                    $("#hart-count").text(count);
                                }else {
                                    $(this).removeClass('icon-hart').addClass('icon-hart-active');
                                    gohartAnimate(this);
                                }
                            }
                        });
                    }
                },'json');
            });
            
            $(".href_page .page_val").keydown(function(){
                if(event.keyCode==13){
                    var tVal = $(this).val();
                    if(tVal != 0 && tVal != undefined && tVal != null && tVal != ''){
                        var li_length = $(this).parent().parent().find('ul.pagination_box').children('li').length;
                        var true_length = eval(li_length) - 2;
                        if(eval(tVal) <= eval(true_length)){
                            var url = $(this).parent().parent().find('ul.pagination_box').children('li').eq(tVal).children('a').attr('href');
                            window.location.href = url;
                        }
                    }
                }

            });
        }else{
        	window.location.href = data.url;
        }
    }
}
//mod by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 end
function gohartAnimate(dom){
    var countSum = parseInt($("#hart-count").text());
    var goodsIcon = $(dom);
    var goodsOffset = goodsIcon.offset();
    var flyIcon = goodsIcon.clone();
    $('body').append(flyIcon);
    countSum++;
    $("#hart-count").text(countSum);
    var count = $('#hart-count').offset();
    flyIcon.css({ 'z-index': 9000,
        'display': 'block',
        'position': 'absolute',
        'top': goodsOffset.top +'px',
        'left': goodsOffset.left +'px',
        'width': goodsIcon.width() +'px',
        'height': goodsIcon.height() +'px'
    });
    flyIcon.animate({
        top: count.top,
        left: count.left,
        width: 10,
        height: 10
    }, 'slow',function(){
        flyIcon.remove()
    });
}

$(function(){

    /* list筛选*/
    $(".js-show-select").bind("click",function(){
        if( $(".select_tool_list").hasClass('hide')){
            $(".select_tool_list").removeClass('hide');
        }else {
            $(".select_tool_list").addClass('hide')
        }
        $(".tool_list .item").click(function(){
            if( $(this).siblings(".item").children().show()){
                $(this).siblings(".item").children(".brand_select_list").hide()
            }

        })
    });
    /* 筛选列表显示 */
    function showtool(box,item){
        var flag = true;
        box.on("click",function(){
            var chageList = $(this).find(item);
            if(flag){
                chageList.fadeIn(300);
                return flag = false
            }else{
                chageList.fadeOut(300);
                return flag=true
            }

            if(chageList.length>0){
                //点击当前品牌填充list
            }
        })
    }
    showtool($("#js_brand"),$("#brand_list"));
    showtool($("#js_list"),$("#fn_list"));
});
// 筛选品类/品牌介绍隐藏
$(".js_link").bind('click',function () {
    if($(this).siblings('.info').is('.show')){
        $(this).siblings('.info').css('height','auto').removeClass('show');
        $(this).children(".click_text").text("点击合并")
    }else {
        $(this).siblings('.info').css('height','3rem').addClass('show');
        $(this).children(".click_text").text("点击展开")
    }
});








